package com.topvision.controller.app;

import com.topvision.dto.VideoStreamDto;
import com.topvision.model.hes.Videostream;
import com.topvision.service.api.glass.AppVideoStreamService;
import com.topvision.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@Api(value = "视频传输相关接口", description = "拍照上传接口")
@RequestMapping("app/videostream")
public class AppVideoStreamController {
    @Autowired
    AppVideoStreamService appVideoStreamService;

    /**
     * 上传文件
     *
     * @param request 请求
     * @param files   文件内容
     * @param guid    设备唯一标识
     * @return 保存后文件路径
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ApiOperation(value = "上传文件", produces = MediaType.APPLICATION_JSON_VALUE, notes = "日志、拍照回调", response = R.class)
    public R uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile[] files,
                        String guid) {
        return appVideoStreamService.uploadFile(request, files, guid);
    }

    @RequestMapping(value = "/confcallback", method = RequestMethod.GET)
    @ApiOperation(value = "视频会议回调接口", produces = MediaType.APPLICATION_JSON_VALUE, notes = "保存视频会议相关信息", response = R.class)
    public R confcallback(Videostream videoStream) {
        // 还未建立会议ID则无需进行保存,跳过
        if (!StringUtils.isEmpty(videoStream.getConfid())) {
            Videostream entity = appVideoStreamService.findByCallFlowId(videoStream.getCallflowid());
            // 添加
            if (entity == null) {
                videoStream.setCreatedtime(new Date());
                videoStream.setEditedtime(new Date());
                appVideoStreamService.saveVideoStreamInfo(videoStream);

            }
            return R.success(videoStream);
        }
        return R.error("confid == null");
    }


    /**
     * 更新视频会议信息内容
     *
     * @param videoStreamDto 订单信息、会议名称等
     * @return
     */
    @RequestMapping(value = "/updateVideoStream", method = RequestMethod.PUT)
    @ApiOperation(value = "更新视频会议信息", produces = MediaType.APPLICATION_JSON_VALUE, notes = "新增订单、公司等信息", response = R.class)
    public R updateVideoStream(@RequestBody VideoStreamDto videoStreamDto) {
        Videostream entity = appVideoStreamService.findByCallFlowId(videoStreamDto.getCallFlowId());
        if (entity != null) {
            entity.setOrderid(videoStreamDto.getOrderId());
            entity.setCompanyid(videoStreamDto.getCompanyId());
            entity.setCallmode(videoStreamDto.getCallMode());
            entity.setConfname(videoStreamDto.getConfName());
            int result = appVideoStreamService.updateVideoStreamInfo(entity);
            if (result > 0) {
                return R.success(entity);
            }
        }
        return R.error("会议未建立成功");
    }
}
